package com.ll.xstream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.io.xml.StaxDriver;
/**
 * Created by daweiLiu on 2016/11/24.
 */
public class XStreamTester {
    public static void main(String args[]){
        XStream xstream = new XStream(new StaxDriver());
        xstream.autodetectAnnotations(true);

        Child student1 = new Child("Mahesh","Parashar");
        Child student2 = new Child("Suresh","Kalra");
        Child student3 = new Child("Ramesh","Kumar");
        Child student4 = new Child("Naresh","Sharma");

        try {
            ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream(new FileOutputStream("test.txt"));
            objectOutputStream.writeObject(student1);
            objectOutputStream.writeObject(student2);
            objectOutputStream.writeObject(student3);
            objectOutputStream.writeObject(student4);
            objectOutputStream.writeObject("Hello World");
            objectOutputStream.close();

            ObjectInputStream objectInputStream = xstream.createObjectInputStream(new FileInputStream("test.txt"));
            Child student5 = (Child)objectInputStream.readObject();
            Child student6 = (Child)objectInputStream.readObject();
            Child student7 = (Child)objectInputStream.readObject();
            Child student8 = (Child)objectInputStream.readObject();
            String text = (String)objectInputStream.readObject();
            System.out.println(student5);
            System.out.println(student6);
            System.out.println(student7);
            System.out.println(student8);
            System.out.println(text);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
@XStreamAlias("child")
class Child {

    private String firstName;
    private String lastName;

    public Child(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String toString(){
        return "Child [ firstName: "+firstName+", lastName: "+ lastName+ " ]";
    }
}